package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.ReportService;
import com.sky.service.WorkSpaceService;
import com.sky.vo.*;
import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private ReportService reportService;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public BusinessDataVO getBusinessData(LocalDate begin,LocalDate end) {

        //LocalDate now = LocalDate.now();
        TurnoverReportVO turnoverReportVO = reportService.turnoverStatistics(begin, end);

        OrderReportVO orderReportVO = reportService.ordersStatistics(begin, end);
        UserReportVO userReportVO = reportService.userStatistics(begin, end);
        String turnoverList = turnoverReportVO.getTurnoverList();
        String[] split = turnoverList.split(",");
        Double turnover=0.0;
        Integer newUsers=0;
        Double unitPrice=0.0;
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        //销售额
        for (String s : split) {
            turnover+=Double.parseDouble(s);
        }

        //新增用户数
        String newUserList = userReportVO.getNewUserList();
        String[] split1 = newUserList.split(",");
        for (String s : split1) {
            newUsers+=Integer.parseInt(s);
        }

        //平均单价
        if(orderReportVO.getValidOrderCount()!=0){
            unitPrice=turnover/orderReportVO.getValidOrderCount();
            unitPrice= Double.valueOf(decimalFormat.format(unitPrice));
        }




        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(orderReportVO.getValidOrderCount())
                .orderCompletionRate(orderReportVO.getOrderCompletionRate())
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();



    }

    @Override
    public OrderOverViewVO overviewOrders() {
        List<OrderVO> list = orderMapper.list(null, null, null);
        Integer waitingOrders = 0, deliveredOrders = 0, completedOrders = 0, cancelledOrders = 0, allOrders = 0;

        if (list != null) {
            for (OrderVO orderVO : list) {
                allOrders++;

                if (orderVO.getStatus() == Orders.TO_BE_CONFIRMED) {
                    waitingOrders++;
                } else if (orderVO.getStatus() == Orders.DELIVERY_IN_PROGRESS) {
                    deliveredOrders++;
                } else if (orderVO.getStatus() == Orders.COMPLETED) {
                    completedOrders++;
                } else if (orderVO.getStatus() == Orders.CANCELLED) {
                    cancelledOrders++;
                }
                // 注意：这里没有else分支，因为上面的条件已经覆盖了所有可能的枚举值
                // 如果将来添加了新的订单状态，您应该在这里添加相应的else if分支
            }
        }

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        List<Dish> list = dishMapper.list(null);
        Integer sold=0,discontinued=0;
        if(list!=null){
            for (Dish dish : list) {
                if(1==dish.getStatus()){
                    sold++;
                }
                else{
                    discontinued++;
                }
            }
        }

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        List<Setmeal> list = setmealMapper.list(null);
        Integer sold=0,discontinued=0;
        if(list!=null){
            for (Setmeal setmeal : list) {
                if(1==setmeal.getStatus()){
                    sold++;
                }
                else{
                    discontinued++;
                }
            }
        }
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
